package cn.pengyinrui.department.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * 邮件发送
 */
@Component
public class EmailUtil {

    @Autowired
    private JavaMailSender javaMailSender;
    @Value("${fromEmail}")
    private String fromEmail;

    public void  send(String email,String emailCode){
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("2712755678@qq.com");
        //邮件主题
        mailMessage.setSubject("激活宠物之家店铺注册");
        //邮件内容
        //mailMessage.setText("http://localhost:8080/shopAuditLog/audit/activation/"+ShopAuditLogId);
        //mailMessage.setText("http://127.0.0.1:8080/shopAuditLog/audit/activation/"+ShopAuditLogId);
        mailMessage.setText("您的邮箱验证码为："+emailCode);
        //收件人
        mailMessage.setTo(email);

        javaMailSender.send(mailMessage);
    }

    public void send2(String title,String url){
        try {
            //创建复杂邮件对象
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            //发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom(fromEmail);
            helper.setSubject(title);
            //第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>宠物之家店铺激活成功</h1><a></a>",true);
            //添加附件
            //helper.addAttachment("战法.jpg",new File("G:\\Awsl\\Tieba\\5F043DFDD46CB26E37CB4CF3881856D0.jpg"));
            //helper.addAttachment("压缩文件", new File("C:\\resources.zip"));
            //收件人
            helper.setTo("3125083970@qq.com");
            //发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}
